import vm from "@/main";

// 用户相关=================================================================

/**
 * 手机号登录
 * @param phone 手机号
 * @param code 验证码
 */
export const phoneLogin = (phone, code) => {
    return vm.$http.post(`/java/user/userController/phoneLogin`, {
            phone, code,
        },
        {emulateJSON: true})
}

/**
 * 密码登录
 * @param phone 手机号
 * @param password 密码
 */
export const passwordLogin = (phone, password) => {
    return vm.$http.post(`/java/user/userController/passwordLogin`, {
            phone, password,
        },
        {emulateJSON: true})
}

/**
 * 退出登录
 */
export const logout = () => {
    return vm.$http.get(`/java/user/userController/loginOut`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 获得验证码
 * @param phone 手机号
 */
export const getCode = (phone) => {
    return vm.$http.get(`/java/user/userController/getCode/${phone}`)
}

/**
 * 获取user对象
 * @param userId 要获取的user对象id
 */
export const getUser = (userId) => {
    return vm.$http.get(`/java/user/userController/getUser/${userId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 获取user对象
 * @param userId 要获取的user对象id
 */
export const getUserByPhone = (phone) => {
    return vm.$http.get(`/java/user/userController/getUserByPhone/${phone}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 获得全部用户
 */
export const getUserByName = (nickname) => {
    return vm.$http.get(`/java/user/userController/getUserByName/${nickname}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/** 
 * 获得活跃用户
 */
export const getActiveUser = () => {
    return vm.$http.get(`/java/user/userController/getActiveUser`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 设置用户
 * @param data 用户数据
 */
export const setUser = (data) => {
    data.phone = (data.phone) ? data.phone : null
    data.avatar = (data.avatar) ? data.avatar : null
    data.nickname = (data.nickname) ? data.nickname : null
    data.password = (data.password) ? data.password : null
    data.fansCount = (data.fansCount) ? data.fansCount : null
    data.loginState = (data.loginState) ? data.loginState : null
    data.followCount = (data.followCount) ? data.followCount : null
    return vm.$http.put(`/java/user/userController/setUser`, {
        id: data.id,
        phone: data.phone,
        nickname: data.nickname,
        password: data.password,
        avatar: data.avatar,
        followCount: data.followCount,
        fansCount: data.fansCount,
        loginState: data.loginState,
    }, {
        headers: {
            token: sessionStorage.getItem("token")
        },
        emulateJSON: false
    })
}

// 好友动态相关===============================================================

/**
 * 通过用户id和动态id获得动态
 * @param userId 用户id
 * @param id 动态id
 */
export const getFriendUpdatesVOByUserIdAndId = (userId, id) => {
    return vm.$http.get(`/java/user/friendUpdatesController/getFriendUpdatesByUserIdAndId/${userId}/${id}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 分页查询
 * @param currentPage 当前页
 * @param pageSize 页大小
 */
export const friendUpdatesPageQuery = (currentPage, pageSize) => {
    return vm.$http.get(`/java/user/friendUpdatesController/pageQuery`, {
            headers: {
                token: sessionStorage.getItem("token")
            },
            params: {
                currentPage, pageSize
            }
        },
        {emulateJSON: true})
}

/**
 * 通过标题获得动态
 * @param title 标题
 */
export const getFriendUpdatesVOByTitle = (title) => {
    return vm.$http.get(`/java/user/friendUpdatesController/getFriendUpdatesVOByTitle/${title}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 通过动态id获得动态
 * @param id 动态id
 */
export const getFriendUpdatesVOById = (id) => {
    return vm.$http.get(`/java/user/friendUpdatesController/getFriendUpdateById/${id}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 通过用户id获得动态
 * @param userId 用户id
 */
export const getFriendUpdatesVOByUserId = (userId) => {
    return vm.$http.get(`/java/user/friendUpdatesController/getFriendUpdateByUserId/${userId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 获得用户收藏的动态
 * @param userId 用户id
 */
export const getCollectionFriendUpdate = (userId) => {
    return vm.$http.get(`/java/user/friendUpdatesController/getCollectionFriendUpdate/${userId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 获得用户点赞数量
 * @param userId 用户id
 */
export const getUserLikeCount = (userId) => {
    return vm.$http.get(`/java/user/friendUpdatesController/getUserLikeCount/${userId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}
/**
 * 保存动态
 * @param data 动态数据
 */
export const friendUpdatesSave = (data) => {
    return vm.$http.post(`/java/user/friendUpdatesController/save`,
        {
            title: data.title,
            styleTitle: data.styleTitle,
            content: data.content,
            styleContent: data.styleContent,
            img: data.img
        },
        {
            headers: {
                token: sessionStorage.getItem("token")
            },
            emulateJSON: true
        })
}

/**
 * 获取当前用户好友动态列表数量
 */
export const friendUpdatesCount = () => {
    return vm.$http.get(`/java/user/friendUpdatesController/count`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 设置动态状态
 * @param data 动态数据
 */
export const setState = (data) => {
    return vm.$http.put(`/java/user/friendUpdatesController/setState`, {
        friendUpdatesId: data.friendUpdatesId,
        hitsState: data.hitsState,
        likeState: data.likeState,
        collectState: data.collectState
    }, {
        headers: {
            token: sessionStorage.getItem("token")
        },
        emulateJSON: false
    })
}

/**
 * 删除动态
 * @param friendUpdatesId 动态id
 */
export const deleteFriendUpdate = (friendUpdatesId) => {
    return vm.$http.delete(`/java/user/friendUpdatesController/deleteFriendUpdate/${friendUpdatesId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}
// 关注列表相关===============================================================

/**
 * 关注用户
 * @param followId 关注用户的id
 */
export const followListFollow = (followId) => {
    return vm.$http.post(`/java/user/followListController/follow/${followId}`,
        {}, {
            headers: {
                token: sessionStorage.getItem("token")
            }
        })
}

/**
 * 通过用户id获得用户关注列表
 * @param userId 用户id
 */
export const getFollowByUserId = (userId) => {
    return vm.$http.get(`/java/user/followListController/getFollow/${userId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 判断是否关注
 * @param userId 用户id
 */
export const isFollow = (userId) => {
    return vm.$http.get(`/java/user/followListController/isFollow/${userId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

// 粉丝相关=================================================================================
/**
 * 通过用户id获得用户关注列表
 * @param userId 用户id
 */
export const getFansByUserId = (userId) => {
    return vm.$http.get(`/java/user/fansListController/getFans/${userId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

// 群聊模块=================================================================================
export const getUserJoinGroup = (userId) => {
    return vm.$http.get(`/java/user/groupController/getUserJoinGroup/${userId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 通过群聊id获得群聊信息
 * @param groupId 群聊id
 */ 
export const getGroupChat = (groupId) => {
    return vm.$http.get(`/java/user/groupController/getGroupChat/${groupId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 通过群聊id获得群聊信息
 * @param groupId 群聊id
 */
export const getGroupInfo = (groupId) => {
    return vm.$http.get(`/java/user/groupController/getGroupInfo/${groupId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 创建群聊
 * @param data 群聊数据
 */
export const createGroup = (data) => {
    return vm.$http.post('/java/user/groupController/createGroup', {
        avatar: data.avatar,
        name: data.name
    }, {
        emulateJSON: true, headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 移除成员
 * @param data 成员数据
 */
export const removeMember = (data) => {
    return vm.$http.delete(`/java/user/groupController/removeMember`, {
        headers: {
            token: sessionStorage.getItem("token")
        },

        params: {
            groupId: data.groupId,
            userId: data.userId
        }
    })
}

// 评论接口=================================================================================
/**
 * 删除评论
 * @param id 评论id
 */
export const deleteComment = (id) => {
    return vm.$http.delete(`/java/user/commentController/delete/${id}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

/**
 * 保存评论
 * @param data 评论数据
 */
export const saveComment = (data) => {
    return vm.$http.post(`/java/user/commentController/save`, {
        userId: data.userId,
        friendUpdatesId: data.friendUpdatesId,
        comment: data.comment
    }, {
        headers: {
            token: sessionStorage.getItem("token")
        },
        emulateJSON: true
    })
}

/**
 * 通过动态id获得评论列表
 * @param friendUpdatesId 动态id
 */
export const getByFriendUpdatesId = (friendUpdatesId) => {
    return vm.$http.get(`/java/user/commentController/getByFriendUpdatesId/${friendUpdatesId}`, {
        headers: {
            token: sessionStorage.getItem("token")
        }
    })
}

// 文件上传=================================================================================
/**
 * 文件上传
 * @param file 文件
 */
export const fileUpload = (file) => {
    return vm.$http.post('/java/user/commonController/upload', file)
}